// pages/video/video.js
import { REQUEST_GROUP_LIST, REQUEST_GROUP } from "../../api/index.js";
import { getStorageSync, showLoading, hideLoading, showToast } from "../../utils/util.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    videoGroupList: [],
    navId: "",
    videoList: [],
    videoId: '',
    videoUpdateTime: [],
    isTriggered: false,
    currentPage: 0,
    hasmore: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getVideoGroupListData();
  },
  async getVideoGroupListData() {
    const { data } = await REQUEST_GROUP_LIST();
    this.setData({
      videoGroupList: data.slice(0, 14),
      navId: data[0].id,
    });
    this.getVideoList(this.data.navId);
  },
  changeNav(evt) {
    const navId = evt.currentTarget.dataset.id;
    this.setData({ navId: navId >>> 0, videoList: [], hasmore: true });
    showLoading();
    this.getVideoList(navId);
  },
  async getVideoList(navId, offset=0) {
    if (!navId) return;
    const cookies = getStorageSync("cookies");
    const MUSIC_U = cookies.length ? cookies.find((item) => item.includes("MUSIC_U")) : "";
    const { datas, hasmore } = await REQUEST_GROUP(
      { id: navId, offset },
      { header: { cookie: MUSIC_U } }
    );
    hideLoading();
    const result = (datas || []).map((item, index) => ({ ...item, id: index }));
    const videoList = [...this.data.videoList, ...result]
    this.setData({
      videoList,
      isTriggered: false,
      hasmore
    });
  },
  handlePlay(evt) {
    const vid = evt.currentTarget.id;
    const { videoUpdateTime } = this.data;
    this.setData({ videoId: vid });
    this.videoCtx = wx.createVideoContext(vid);
    const videoItem = videoUpdateTime.find(item => item.vid === vid);
    videoItem && this.videoCtx.seek(videoItem.currentTime);
  },
  handleTimeUpdate(evt) {
    const { videoUpdateTime } = this.data;
    const videoObject = { vid: evt.currentTarget.id, currentTime: evt.detail.currentTime };
    const videoItem = videoUpdateTime.find(item => item.vid === videoObject.vid);
    if (videoItem) videoItem.currentTime = evt.detail.currentTime
    else videoUpdateTime.push(videoObject);
    this.setData({ videoUpdateTime });
  },
  bindEnded(evt) {
    const { videoUpdateTime } = this.data;
    const index = videoUpdateTime.findIndex(item => item.vid === evt.currentTarget.id);
    videoUpdateTime.splice(index, 1);
    this.setData({
      videoUpdateTime
    });
  },
  handleRefresher() {
    this.getVideoList(this.data.navId);
  },
  handleToLower() {
    if (!this.data.hasmore) return showToast({title: '没有更多数据了'});
    this.setData({ currentPage: this.data.currentPage += 1 });
    const { navId, currentPage } = this.data;
    this.getVideoList(navId, currentPage)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
